<template>
<div class="main_box"  style="padding:10px">
  <div class="add_wrap">
    <div class="tit">
      <h2>{{ $route.query.id ? '编辑' : '新增'}}设备</h2>
      <el-button type="success" plain size="small" icon="fa fa-mail-forward" @click="returnList">返回</el-button>
    </div>
    <el-form :model="dataForm" ref="dataForm" label-width="200px" :rules="roleRules">
      <el-row>

        <el-form-item label="标本箱名称" prop="boxName">
          <el-input v-model="dataForm.boxName" @keydown.native="keydown($event)"></el-input>
        </el-form-item>
        <el-form-item label="标本箱编号" prop="boxCode">
          <el-input v-model="dataForm.boxCode" @keydown.native="keydown($event)"></el-input>
        </el-form-item>
        <el-form-item label="MAC地址" prop="boxMac">
          <el-input v-model="dataForm.boxMac" @keydown.native="keydown($event)"></el-input>
        </el-form-item>
        <el-form-item label="标本箱类型">
          <el-select v-model="dataForm.boxType" placeholder="请选择">
            <el-option label="一代" :value="1"></el-option>
            <el-option label="二代" :value="2"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="服务员" prop="waiterId">
          <el-select v-model="dataForm.waiterId" placeholder="请选择" clearable filterable>
            <el-option v-for="item in waiterList" :label="item.name" :value="item.id" :key="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="温度最低报警值" prop="tempMin">
          <el-input type="number"  v-model.number="dataForm.tempMin"></el-input>
        </el-form-item>
        <el-form-item label="温度最高报警值" prop="tempMax">
          <el-input type="number" v-model.number="dataForm.tempMax"></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input type="textarea" v-model.number="dataForm.remark"></el-input>
        </el-form-item>
      </el-row>

      <el-row>
        <el-form-item>
          <el-button plain type="primary" @click="sureClick">保 存</el-button>
          <el-button plain @click="returnList">取 消</el-button>
        </el-form-item>
      </el-row>
    </el-form>
  </div>
 </div>
</template>

<script>
import * as message from "@/constants/message";
import WaiterApi from "@/api/business/waiter";
import SpecimenApi from '@/api/equip/specimen';
export default{
  data(){
      return {
        dataForm:{
          boxId: "",
          boxName: "",
          boxCode: "",
          boxMac:"",
          waiterId: "",
          boxType: "",
          tempMin:"", // 温度最低报警值
          tempMax:"",// 温度最高报警值
          remark:"",
        },
        waiterList:[],
        loadingIns:false,
        temperatureList: [],
        roleRules: {
          boxName: [
            { required: true, message: "请输入标本箱名称", trigger: "blur" },
            { max: 30, message: '长度不超过30个字', trigger: 'blur' }
          ],
          boxCode: [
            { required: true, message: "请输入标本箱编号", trigger: "blur" },
            { max: 20, message: '长度不超过20个字', trigger: 'blur' }
          ],
        }
      }
  },
  components: {
  },
  methods: {
    init(){
      if (this.$route.query.id) {
        this.dataForm.boxId = this.$route.query.id
        this.getData()
      }
      this.getWaiterList()
    },
    keydown(e) {
      if (e.keyCode == 32) {
        e.returnValue = false;
      }
    },
    async getWaiterList(){
      const {data} = await WaiterApi.listIdName()
      this.waiterList = data||[]
    },
    sureClick() {
      this.$refs.dataForm.validate(valid => {
        if (valid) {
          this.$confirm(message.MSG_CONFIRM_SAVE_ALTER, message.TITLE_PROMPT, {})
            .then(() => {
              if (this.dataForm.boxId){
                this.editData();
              }else{
                this.addData();
              }
          });
        }
      })
    },
    addData() {
     let param = {
          boxName: this.dataForm.boxName,
          boxCode: this.dataForm.boxCode,
          boxMac:this.dataForm.boxMac,
          boxType: this.dataForm.boxType,
          waiterId:this.dataForm.waiterId,
          tempMin:this.dataForm.tempMin,
          tempMax:this.dataForm.tempMax,
          remark:this.dataForm.remark,
      };
      SpecimenApi.addSpecimen(param)
      .then(data => {
        this.$Utils.notifySuccess()
        this.returnList();
      })
    },
    editData() {
      let param = {
        boxId:this.data.boxId,
        boxName: this.dataForm.boxName,
        boxCode: this.dataForm.boxCode,
        boxMac:this.dataForm.boxMac,
        boxType: this.dataForm.boxType,
        waiterId:this.dataForm.waiterId,
        tempMin:this.dataForm.tempMin,
        tempMax:this.dataForm.tempMax,
        remark:this.dataForm.remark,
      };
      SpecimenApi.editSpecimen(param)
        .then(data => {
          this.$Utils.notifySuccess()
          this.returnList();
        })
    },
    getData () {
      SpecimenApi.getSpecimenInfo({
        id: this.dataForm.boxId
      }).then(res => {
        this.$Utils.bindingBean(this.dataForm, res.data)
      })
    },
    returnList(){
      this.$store.dispatch('delView', this.$route);
      this.$router.push({path: '/equip/specimenManagement'});
    }
  },
  mounted(){
    this.init()
  }
}
</script>
<style lang="scss" scoped>
@import "@/assets/css/views/add.scss";
</style>
